﻿/*
Copyright (C) 2009 Dumitru Condrea

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// CONFIGURATION - EDIT FOLLOWING:
var latestversionxmlURL="http://sites.google.com/site/dumitrucondrea/google-desktop-gadgets/latestversion.xml"
var gadget_version_number="1.0.0.8";

function runcheckupdate() { checkupdate(true) }

function checkupdate(manualrequest)
{
  //load xml code
  var updatexmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  function updateloadXML(uxmlFile) 
  {
    updatexmlDoc.async="false";
    updatexmlDoc.onreadystatechange=verify;
    updatexmlDoc.load(uxmlFile);
    updatexmlObj=updatexmlDoc.documentElement;
  }

  function verify() 
  {
    if (updatexmlDoc.readyState != 4) 
    {
      return false;
    }
  }

  //try to load latestversion.xml and parse file
  try 
  {
    updateloadXML(latestversionxmlURL);
    //set xml version info to variables
    latestVis=updatexmlObj.childNodes(0).childNodes(1).text
    changesinvertitle = updatexmlObj.childNodes(0).childNodes(2).childNodes(0).text
    changesinverdesc = updatexmlObj.childNodes(0).childNodes(2).childNodes(1).text
    downloadurl = updatexmlObj.childNodes(0).childNodes(2).childNodes(2).text
    //
    //This is where we will later put the code to alert the user of an update.
    //
if (gadget_version_number != latestVis) 
{
  //IF THERE IS A NEW VERSION TELL THE USER
  alert("A newer version of \""+GADGET_NAME+"\" is available.\nIt is recommended that you update.\n\n" + 
  "You currently have version "+gadget_version_number +
        " and the latest version is "+latestVis+".\n\n"+changesinvertitle+"\n"+changesinverdesc)
  //ask if they want to download
  if (confirm('Do you wish to go to the gadget download website?')) 
  {
    var winShell = new ActiveXObject("Shell.Application");
    winShell.ShellExecute(downloadurl); 
  }
}
//if there is no update, and the user has manually asked tell them
else if(manualrequest) //Once again, this will be explained later.
{
  alert("You have the latest version.\n\nYou currently have version "+gadget_version_number +
        " and the latest version is "+latestVis+".")
}

  } 
  catch (e) 
  {
    // if anything above raised an error (downloading or parsing), only alert user if it was a manual, user-initiated request. 
    // this will be explained later. 
    if (manualrequest) 
    {
      alert("Error checking for updates. Please check your network connection.")
    }
  }
}
